#!/bin/sh

DEVICE=/dev/tpm0

# Check that the device is owned by root.
PERMISSIONS=$(stat -c "%u %a" $DEVICE)
EXPECTED="0 660"
if [ "$PERMISSIONS" != "$EXPECTED" ]; then
  echo "Failed: $DEVICE permission $PERMISSIONS do not match $EXPECTED"
  exit 1
fi

# Try to start tcsd if it wasn't started.
/usr/sbin/tpm_startup || true
/etc/init.d/trousers start

TCSD=$(ps | grep tcsd | awk '{print $1}')
if [ "$TCSD" == "" ]; then
  echo "Failed: tcsd daemon is not running via /etc/init.d/trousers"
  exit 1
fi

NVRAM_INDEXES=$(/usr/sbin/tpm_nvinfo | grep "NVRAM" | awk '{print $4}')
EXPECTED=$(echo -e "0x30000001\n0x10000001\n0x1000f000")
for index in $EXPECTED; do
  FOUND=$(echo $NVRAM_INDEXES | grep $index)
  if [ "$FOUND" == "" ]; then
    echo "Failed: Cannot find NVRAM index: $index"
    exit 1
  fi
done
